package com.beordie.dao.impl;

import com.beordie.dao.IMiddleMenuDao;
import com.beordie.utils.DBUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname MiddleMenuDaoImpl
 * @Description TODO
 * @Date 2021/6/21 12:03
 * @Created 30500
 */
public class MiddleMenuDaoImpl extends DBUtils implements IMiddleMenuDao {
    @Override
    public int[] add(int roleId, int[] menuId) {
        String sql = "insert into middle values(null,?,?)";
        int[] result = null;
        statement = getStatement(sql);
        try {
            for (int id : menuId) {
                statement.setInt(1, roleId);
                statement.setInt(2, id);
                statement.addBatch();
            }
            result = statement.executeBatch();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.close();
        }
        return result;
    }

    @Override
    public int delete(int roleId) {
        String sql = "delete from middle where roleid=?";
        List<Integer> params = new ArrayList<>();

        params.add(roleId);
        int result = 0;
        try {
            result = super.update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            super.close();
        }
        return result;
    }
}
